package com.ftwj.demo.generate.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
//import java.io.StringWriter;

import com.ftwj.demo.generate.entity.Configuration;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FileUtil {

	public static void generateToJava(String type, Object data, String fileName) throws IOException, TemplateException {
		Template tpl = getTemplate(type); // 获取模板文件
		// 填充数据
		/**
		StringWriter writer = new StringWriter();
		tpl.process(data, writer);
		writer.flush();
		*/
		// 写入文件
		String dir=getPackagePath(type);
		if(dir==null||dir.trim().isEmpty()) {
			dir=getSourcePath();
		}
		File file=new File(dir);
		if(!file.exists()&&file.mkdirs());
		FileOutputStream fos = new FileOutputStream(dir+fileName);
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		BufferedWriter bw = new BufferedWriter(osw, 1024);
		tpl.process(data, bw);
		fos.close();
	}

	/**
     * 
     * <p>Title: getTemplate</p>  
     * <p>Description: 获取模板</p>  
     * @param type
     * @return
     * @throws IOException 
     * @author WangsLi
     * @date 2020年2月28日  
     * @version 1.0
     */
    private static Template getTemplate(String type) throws IOException {
        switch (type) {
            case "ENTITY":
                return FreemarkerConfigUtil.getInstance().getTemplate("entity.ftl");
            case "DAO":
                return FreemarkerConfigUtil.getInstance().getTemplate("dao.ftl");
            case "SERVICE":
                return FreemarkerConfigUtil.getInstance().getTemplate("service.ftl");
            case "CONTROLLER":
                return FreemarkerConfigUtil.getInstance().getTemplate("controller.ftl");
            case "JAVASCRIPT":
                return FreemarkerConfigUtil.getInstance().getTemplate("javascript.ftl");
            case "HTML":
                return FreemarkerConfigUtil.getInstance().getTemplate("html.ftl");
            default:
                return null;
        }
    }
    
    /**
     * 
     * <p>Title: getPackagePath</p>  
     * <p>Description: </p>  
     * @param type
     * @return
     * @throws IOException 
     * @author WangSLi
     * @date 2020年3月21日  
     * @version 1.0
     */
    private static String getPackagePath(String type) throws IOException {
    	Configuration config = ConfigUtil.getConfiguration();
    	String basePath=ConfigUtil.getConfiguration().getPath()+StringUtil.package2Path(ConfigUtil.getConfiguration().getPackageName());
        switch (type) {
        	case "ENTITY":
                return basePath+StringUtil.package2Path(config.getFile().getEntity());
        	case "DAO":
                return basePath+StringUtil.package2Path(config.getFile().getDao());
        	case "SERVICE":
                return basePath+StringUtil.package2Path(config.getFile().getService());
        	case "CONTROLLER":
                return basePath+StringUtil.package2Path(config.getFile().getController());
        	case "JAVASCRIPT":
                return basePath+StringUtil.package2Path(config.getFile().getJavascript());
        	case "HTML":
                return basePath+StringUtil.package2Path(config.getFile().getHtml());
            default:
                return "";
        }
    }
    
    private static String getBasicProjectPath() {
        String path = new File(FileUtil.class.getClassLoader().getResource("").getFile()).getPath() + File.separator;
        StringBuilder sb = new StringBuilder();
        sb.append(path.substring(0, path.indexOf("target"))).append("src").append(File.separator).append("main").append(File.separator);
        return sb.toString();
    }

    /**
     * 	获取源码路径
     *
     * @return
     */
    public static String getSourcePath() {
        StringBuilder sb = new StringBuilder();
        sb.append(getBasicProjectPath()).append("java").append(File.separator);
        return sb.toString();
    }

    /**
     * 	获取资源文件路径
     *
     * @return
     */
    public static String getResourcePath() {
        StringBuilder sb = new StringBuilder();
        sb.append(getBasicProjectPath()).append("resources").append(File.separator);
        return sb.toString();
    }


}
